package com.tsingteng.cosfun.ui.setting;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.base.BasePresenterActivity;
import com.tsingteng.cosfun.bean.ProfileAdressBean;
import com.tsingteng.cosfun.ui.setting.contract.ProfileAdress;
import com.tsingteng.cosfun.ui.setting.presenter.ProfileAdressPresenter;
import com.tsingteng.cosfun.utils.NotchUtil;
import com.tsingteng.cosfun.widget.picker.AddressPickTask;
import com.tsingteng.cosfun.widget.picker.City;
import com.tsingteng.cosfun.widget.picker.County;
import com.tsingteng.cosfun.widget.picker.Province;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 收货地址界面
 * author zht
 */
public class ProfileChangeAdressActivity extends BasePresenterActivity<ProfileAdressPresenter, ProfileAdress.IProfileView> implements  ProfileAdress.IProfileView ,View.OnClickListener{

    @BindView(R.id.back_iv)
    ImageView mBackIv;
    @BindView(R.id.adress_area_ll)
    LinearLayout mAdressAreaLl;
    @BindView(R.id.prefile_et)
    EditText mPrefileEt;
    @BindView(R.id.phone_et)
    EditText mPhoneEt;
    @BindView(R.id.adress_tv)
    TextView mAdressTv;
    @BindView(R.id.adress_et)
    EditText mAdressEt;
    @BindView(R.id.adress_save_tv)
    TextView mAdressSaveTv;
    @BindView(R.id.adress_area_view)
    View mAdressAreaView;
    private String mAddress;
    private long mId;
    private String mUserName;
    private String name;
    private String phone;
    private String region;
    private String mAdress;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_profile_change_adress;
    }

    @Override
    protected void getIntentData(Intent intent) {
        isFound = true;
        NotchUtil.setWindowTrans(this, false, false);
       name= intent.getStringExtra("name");
       phone= intent.getStringExtra("phone");
       region= intent.getStringExtra("region");
        mAdress= intent.getStringExtra("adress");
    }

    @Override
    protected void initData() {
        mPrefileEt.setText(name);
        mPhoneEt.setText(phone);
        mAdressTv.setText(region);
        mAdressEt.setText(mAdress);
        mBackIv.setOnClickListener(this);
        mPrefileEt.addTextChangedListener(textWatcher);
        mAdressEt.addTextChangedListener(textWatcherAdress);
        mPresenter.getReceiveAddress();
    }

    private TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            if (mPrefileEt.length() == 10) {
                showToast(getResources().getString(R.string.profile_most));
            }
        }
    };
    private TextWatcher textWatcherAdress = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            if (mAdressEt.length() == 100) {
                showToast(getResources().getString(R.string.adress_most));
            }
        }
    };

    @Override
    protected ProfileAdressPresenter createPresenter() {
        return new ProfileAdressPresenter();
    }


    @OnClick({R.id.adress_area_ll, R.id.adress_save_tv,R.id.back_iv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
                finish();
                break;
            case R.id.adress_area_ll:
                onAddressPicker();
                break;
            case R.id.adress_save_tv:
                String mPrefile = mPrefileEt.getText().toString().trim();
                String mPhone = mPhoneEt.getText().toString().trim();
                String mAdressDetail = mAdressEt.getText().toString().trim();
                String mCityAdress = mAdressTv.getText().toString().trim();
                if (TextUtils.isEmpty(mPrefile)) {
                    showToast(getResources().getString(R.string.name_not_empty));
                    return;
                }
                if (TextUtils.isEmpty(mPhone)) {
                    showToast(getResources().getString(R.string.phone_not_empty));
                    return;
                }
                if (mPhone.length() != 11) {
                    showToast(getResources().getString(R.string.phone_not_right));
                    return;
                }
                if (TextUtils.isEmpty(mAdressDetail)) {
                    showToast(getResources().getString(R.string.adress_not_empty));
                    return;
                }
                if (TextUtils.isEmpty(mCityAdress) || mCityAdress.equals(getResources().getString(R.string.city_county))) {
                    showToast(getResources().getString(R.string.city_choice));
                    return;
                }
                if (TextUtils.isEmpty(mUserName)) {
                    mPresenter.saveReceiceAddress(mPrefile, mPhone, mCityAdress, mAdressDetail);
                } else {
                    mPresenter.updateReceiceAddress(mId, mPrefile, mPhone, mCityAdress, mAdressDetail);
                }
                break;
        }
    }

    /**
     * 城市选择滚轮
     */
    public void onAddressPicker() {
        AddressPickTask task = new AddressPickTask(this);
        task.setHideProvince(false);
        task.setHideCounty(false);
        task.setCallback(new AddressPickTask.Callback() {
            @Override
            public void onAddressInitFailed() {
                showToast("数据初始化失败");
            }

            @Override
            public void onAddressPicked(Province province, City city, County county) {
                if (county == null) {
                    mAddress = city.getAreaName();
                } else {
                    if (province.getAreaName().equals(city.getAreaName())) {
                        mAddress = city.getAreaName() + county.getAreaName();
                    } else {
                        mAddress = province.getAreaName() + city.getAreaName() + county.getAreaName();
                    }
                }
                mAdressTv.setText(mAddress);
            }
        });
        task.execute("北京", "北京", "东城区");
    }

    @Override
    public void showProfileAdress() {
        showToast(getResources().getString(R.string.save_adress_success));
        finish();
    }

    @Override
    public void showReceiveAddress(ProfileAdressBean profileAdressBean) {
        if (null == profileAdressBean.getReceiveAddress()) {
            return;
        }

    }
}
